﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OutillageTests;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;

namespace GestionnaireTUPleiade
{
    public partial class FormGestionCouvertureTU : Form
    {
        private Core _core = Core.GetInstance();
        
        public FormGestionCouvertureTU()
        {
            InitializeComponent();
        }

        private void FormGestionCouvertureTU_Load(object sender, EventArgs e)
        {
            label1.Text = _core.GetBusinessAssemblyPath();
            
            if (_core.GetBusinessMap().Count == 0)
                _core.LoadBusinessMap("Adp.Pleiade.Metier*.Services.dll");

            // Remplissage de la TreeView avec les éléments de plus haut niveau
            foreach (var classe in _core.GetBusinessMap())
            {
                string[] cutString = classe.Key.Namespace.Split(new char[] {'.'});
                string domainNodeKey = cutString[3].ToUpper();
                string subSystemNodeKey = cutString[4].ToUpper();
                string articleNodeKey = "SANS ARTICLE";
                if (cutString.Length == 6) 
                    articleNodeKey = cutString[5].ToUpper();

                // Premier noeud : le domaine
                if (!tvSelectionMetier.Nodes.ContainsKey(domainNodeKey))
                    tvSelectionMetier.Nodes.Add(domainNodeKey, domainNodeKey);

                // Second noeud : le sous-système
                if (!tvSelectionMetier.Nodes[domainNodeKey].Nodes.ContainsKey(subSystemNodeKey))
                    tvSelectionMetier.Nodes[domainNodeKey].Nodes.Add(subSystemNodeKey, subSystemNodeKey);

                // Troisième noeud : l'article
                if (!tvSelectionMetier.Nodes[domainNodeKey].Nodes[subSystemNodeKey].Nodes.ContainsKey(articleNodeKey))
                    tvSelectionMetier.Nodes[domainNodeKey].Nodes[subSystemNodeKey].Nodes.Add(articleNodeKey, articleNodeKey);
            
            }
            tvSelectionMetier.Refresh();

            // Mise à jour de la TreeView avec les tous les éléments
            foreach (var classe in _core.GetBusinessMap())
            {
                string[] cutString = classe.Key.Namespace.Split(new char[] { '.' });
                string domainNodeKey = cutString[3].ToUpper();
                string subSystemNodeKey = cutString[4].ToUpper();
                string articleNodeKey = "SANS ARTICLE";
                if (cutString.Length == 6)
                    articleNodeKey = cutString[5].ToUpper();
                string className = classe.Key.Name;

                TreeNode newnode = new TreeNode(className);
                newnode.ToolTipText = "Classe " + className;
                tvSelectionMetier.Nodes[domainNodeKey].Nodes[subSystemNodeKey].Nodes[articleNodeKey].Nodes.Add(newnode);
                tvSelectionMetier.Nodes[domainNodeKey].Nodes[subSystemNodeKey].Nodes[articleNodeKey].Nodes[newnode.Index].ImageIndex = 1;

                foreach (var method in classe.Value)
                {                     
                    // On ne garde que les méthodes identifiées comme testables
                    if (method.Key != null && method.Value)
                    {
                        TreeNode newNode = new TreeNode(method.Key.Name);
                        newNode.ToolTipText = Tools.DonnerChaineDeclarationMethode(method.Key);

                        tvSelectionMetier.Nodes[domainNodeKey].Nodes[subSystemNodeKey].Nodes[articleNodeKey].Nodes[newnode.Index].Nodes.Add(newNode);
                        if (method.Key.IsPublic)
                            tvSelectionMetier.Nodes[domainNodeKey].Nodes[subSystemNodeKey].Nodes[articleNodeKey].Nodes[newnode.Index].Nodes[newNode.Index].ImageIndex = 2;
                        else if (method.Key.IsPrivate)
                            tvSelectionMetier.Nodes[domainNodeKey].Nodes[subSystemNodeKey].Nodes[articleNodeKey].Nodes[newnode.Index].Nodes[newNode.Index].ImageIndex = 4;
                        else
                            tvSelectionMetier.Nodes[domainNodeKey].Nodes[subSystemNodeKey].Nodes[articleNodeKey].Nodes[newnode.Index].Nodes[newNode.Index].ImageIndex = 3;

                    }
                }
            }
            tvSelectionMetier.Refresh();
        }

        private void tvSelectionMetier_AfterSelect(object sender, TreeViewEventArgs e)
        {
            tvSelectionMetier.ImageList = imageList1;
        }

        // Gestion du CMS principal
        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (tvSelectionMetier.SelectedNode == null)
                contextMenuStrip1.Visible = false;
            else
                contextMenuStrip1.Visible = true;
        }

        private void tvSelectionMetier_AfterCheck(object sender, TreeViewEventArgs e)
        {
            TreeNode actualNode = e.Node;
            if (actualNode.GetNodeCount(false) > 0)
                CheckerSousNoeuds(actualNode);
        }

        private void CheckerSousNoeuds(TreeNode noeudParent)
        {
            foreach (TreeNode sousNoeud in noeudParent.Nodes)
            {
                TreeNode actualNode = sousNoeud;
                actualNode.Checked = noeudParent.Checked;
                if (actualNode.GetNodeCount(false) > 0)
                    CheckerSousNoeuds(actualNode);
            }
        }
    }
}
